package com.asion.vmovie.adapters;

import android.animation.ObjectAnimator;
import android.content.Context;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;

import com.asion.vmovie.R;
import com.asion.vmovie.beans.ChannalsBean;
import com.bumptech.glide.Glide;
import com.bumptech.glide.request.animation.ViewPropertyAnimation;

import java.util.ArrayList;
import java.util.Collection;
import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;

/**
 * Created by asion on 16-10-18.
 */

public class PingDaoRecyclerAdapter extends RecyclerView.Adapter<PingDaoRecyclerAdapter.MyPingHolder> {

    private Context context;
    private List<ChannalsBean.DataBean> list;

    public PingDaoRecyclerAdapter(Context context) {
        this.context = context;
        list = new ArrayList<>();
    }

    private OnItemClickListener onItemClickListener;

    public void setOnItemClickListener(OnItemClickListener onItemClickListener) {
        this.onItemClickListener = onItemClickListener;
    }

    @Override
    public MyPingHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        View view = LayoutInflater.from(context).inflate(R.layout.pingdao_item, parent, false);
        return new MyPingHolder(view);
    }

    @Override
    public void onBindViewHolder(MyPingHolder holder, int position) {
        ChannalsBean.DataBean dataBean = list.get(position);
        Glide.with(context).load(dataBean.getIcon()).asBitmap()
                .error(R.mipmap.default_error)
                .animate(animationObject)
                .centerCrop()
                .into(holder.pingdaoImage);
        holder.cate.setText("#"+dataBean.getCatename()+"#");
        holder.itemView.setOnClickListener(v -> onItemClickListener.onItemClick(dataBean));
    }

    @Override
    public int getItemCount() {
        return list.size();
    }

    public void addAll(Collection collection){
        list.addAll(collection);
        notifyDataSetChanged();
    }

    public void removeAll(){
        list.clear();
        notifyDataSetChanged();
    }

    public static class MyPingHolder extends RecyclerView.ViewHolder {

        @BindView(R.id.pingdao_image)
        ImageView pingdaoImage;
        @BindView(R.id.cate)
        TextView cate;

        public MyPingHolder(View itemView) {
            super(itemView);
            ButterKnife.bind(this,itemView);
        }
    }

    public interface OnItemClickListener{
        void onItemClick(ChannalsBean.DataBean dataBean);
    }

    ViewPropertyAnimation.Animator animationObject =view -> {
            view.setAlpha( 0f );
            ObjectAnimator fadeAnim = ObjectAnimator.ofFloat( view, "alpha", 0f, 1f );
            fadeAnim.setDuration(1000);
            fadeAnim.start();
    };
}
